package com.yonyou.brigade.cron.user.ctripSME;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.basic_init.model.Developer;
import com.yonyou.brigade.basic_init.service.IBriChannelService;
import com.yonyou.brigade.basic_init.service.IDeveloperService;
import com.yonyou.brigade.third_parties.smectrip.service.ISmeCtripUserService;
import com.yonyou.ybzboot.starter.elasticjob.lite.annotation.ElasticJobConfig;
import com.yonyou.ybzboot.starter.mail.model.Email;
import com.yonyou.ybzboot.starter.mail.service.IMailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

@ElasticJobConfig(cron = "0 0 5 * * ?", shardingTotalCount = 1)
public class SyncCtripSMEUserJob implements SimpleJob {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IBriChannelService briChannelService;

    @Autowired
    private ISmeCtripUserService smeCtripUserService;

    @Autowired
    private IDeveloperService developerService;

    @Autowired
    private IMailService mailService;

    @Value("${brigade.cur}")
    private String curEnv;

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("<<<<ctrip-sme-sync-users-begin>>>>");
        List<BriChannel> briChannels = briChannelService.getEnableChannelByChannel("ctripSME");
        if (briChannels != null && briChannels.size() > 0) {
            briChannels.forEach(briChannel -> {
//                if (briChannel != null && BriChannel.NEEDSYNCUSER == briChannel.getNeedSyncUser()) {
                    String tenant = briChannel.getTenant();
                    String config = briChannel.getConfig();
                    String phone = briChannel.getPhone();
                    try {
                        if (BriChannel.SYNCUSER == briChannel.getSyncUser()) {
                            smeCtripUserService.syncAllUsersToCtripSME(tenant, config, phone);
                        }
                    } catch (Exception e) {
                        Wrapper<Developer> developerWrapper = new EntityWrapper<>();
                        developerWrapper.where("type={0}", "KF");
                        List<Developer> developers = developerService.selectList(developerWrapper);
                        String developerEmails = "";
                        for (int j = 0; j < developers.size(); j++) {
                            developerEmails += developers.get(j).getEmail();
                            if (j < developers.size() - 1) {
                                developerEmails += ",";
                            }
                        }
                        Email email = new Email();
                        String[] emails = developerEmails.split(",");
                        email.setEmail(emails);
                        email.setSubject(curEnv + "环境携程商旅通同步人员失败");
                        email.setContent(e.getMessage());
                        try {
                            log.error(e.getMessage());
                            mailService.send(email);
                        } catch (Exception e1) {
                            log.error("提示" + curEnv + "环境携程商旅通同步人员失败的邮件发送失败！" + e1.getMessage());
                        }
                    }
//                }
            });
        }
        log.info("<<<<ctrip-sme-sync-users-end>>>>");
    }
}
